package com.ddstation.crm.institution.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddstation.crm.institution.entity.DtHealthManageInstitutionBuyAccountEntity;
import com.ddstation.crm.institution.entity.DtHealthManageInstitutionEntity;
import com.ddstation.crm.institution.mapper.DtHealthManageInstitutionDoctorMapper;
import com.ddstation.crm.institution.mapper.DtHealthManageInstitutionMapper;
import com.ddstation.crm.institution.model.UserAccountModel;

@Service
public class InstitutionServiceImpl implements InstitutionService {

	@Override
	@Transactional(readOnly=true)
	public DtHealthManageInstitutionEntity get(String institutionId) {
		// TODO Auto-generated method stub
		return dtHealthManageInstitutionMapper.select(institutionId);
	}
	
	@Override
	@Transactional(readOnly=true)
	public boolean dumplicateName(String institutionName) {
		// TODO Auto-generated method stub
		List<DtHealthManageInstitutionEntity> institutions = dtHealthManageInstitutionMapper.selectList(institutionName);
		if (institutions != null && !institutions.isEmpty()) {
			for (DtHealthManageInstitutionEntity institution : institutions) {
				if (institution.getName().equals(institutionName) && institution.getExamineVerifySt() == 20) { // 已审核并且同名
					return true;
				}
			}
		}
		return false;
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean save(DtHealthManageInstitutionEntity institution) {
		// TODO Auto-generated method stub
//		if (institution.getPaySt() != null && institution.getPaySt() != 1 && institution.getPaySt() != 2 && institution.getPaySt()!= 3 && institution.getPaySt() != 4) {
//			throw new IllegalStateException("支付类型不正确");
//		}
		return dtHealthManageInstitutionMapper.update(institution) == 1;
	}
	
	/**
	 * 检索机构的失效日期
	 * @param institutionArray
	 * @return
	 */
	@Override
	@Transactional(readOnly=true)
	public List<DtHealthManageInstitutionBuyAccountEntity> getEndDt(List<String> institutionArray) {
		// TODO Auto-generated method stub
		return dtHealthManageInstitutionDoctorMapper.selectInstitutionEndDt(institutionArray);
	}
	
	@Override
	@Transactional(readOnly=true)
	public List<UserAccountModel> getAccounts(String institutionId) {
		// TODO Auto-generated method stub
		List<UserAccountModel> accounts = dtHealthManageInstitutionDoctorMapper.selectInstitutionDoctor(institutionId);
//		if (accounts != null && !accounts.isEmpty()) {
//			Iterator<UserAccountModel> accountIte = accounts.iterator();
//			while (accountIte.hasNext()) {
//				UserAccountModel account = accountIte.next();
//				if (account.getAuthType() != 4) {
//					accountIte.remove();
//				}
//			}
//		}
		return accounts;
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public void insertTestsetManage(String institutionId){
//		// 创建dt_test_set_manage数据
//        DtTestSetManageEntity testSet = new DtTestSetManageEntity();
//        testSet.setInstitutionId(institutionId);
//        testSet.setCostRate(new BigDecimal(0.6));
//        testSet.setSiteCode("K0107");
//        testSet.setUnifyCodeFlg(1);
//        testSet.setCustomerCode("33002092");
//        testSet.setCollectionLocation("33002092|001");
//        testSet.setSpecimenBoxCode("33A1B000003");
//        testSet.setLegworkNo("016203");
//        testSet.setProvince(12);//浙江
//        testSet.setCity(208);//杭州
//        testSet.setCounty(1322);//滨江
//        testSet.setTestInstitution(0);
//        dtTestSetManageMapper.insertSelective(testSet);
	}

	/**
	 * 查询自身以外的机构是否有同名
	 * */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int searchSameNameCnt(DtHealthManageInstitutionEntity _a){
		return dtHealthManageInstitutionMapper.selectSameNameCnt(_a);
	}
	
	@Inject private DtHealthManageInstitutionMapper dtHealthManageInstitutionMapper = null;
	@Inject private DtHealthManageInstitutionDoctorMapper dtHealthManageInstitutionDoctorMapper = null;
}